﻿#region MIT
/*
Blue.ResourceManager
Part of Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Blue.ResourceManager
{
    public class ResourceCreationStream : Stream
    {
        #region Variables
        Stream _wrappedStream;
        String _resourceName;
        bool _disposed;
        bool _resourceCreationAborted;
        #endregion

        #region Constructor / Finalizer
        internal ResourceCreationStream( 
            Stream wrappedStream, 
            String resourceName )
        {
            Debug.Assert( wrappedStream != null );
            _wrappedStream = wrappedStream;
            _resourceName = resourceName;
        }

        ~ResourceCreationStream()
        {
            Dispose( false );
        }
        #endregion

        #region Properties
        public String ResourceName
        {
            get { return _resourceName; }
        }

        public bool ResourceCreationAborted
        {
            get { return _resourceCreationAborted; }
            set { _resourceCreationAborted = value; }
        }

        public override bool CanRead
        {
            get { return _wrappedStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return _wrappedStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return _wrappedStream.CanWrite; }
        }

        public override long Length
        {
            get { return _wrappedStream.Length; }
        }

        public override long Position
        {
            get
            {
                return _wrappedStream.Position;
            }
            set
            {
                _wrappedStream.Position = value;
            }
        }
        #endregion

        #region Methods
        public override void Flush()
        {
            _wrappedStream.Flush();
        }

        public override int Read( byte[] buffer, int offset, int count )
        {
            return _wrappedStream.Read( buffer, offset, count );
        }

        public override long Seek( long offset, SeekOrigin origin )
        {
            return _wrappedStream.Seek( offset, origin );
        }

        public override void SetLength( long value )
        {
            _wrappedStream.SetLength( value );
        }

        public override void Write( byte[] buffer, int offset, int count )
        {
            _wrappedStream.Write( buffer, offset, count );
        }

        protected override void Dispose( bool disposing )
        {
            if ( !_disposed )
            {
                if ( disposing )
                {
                    if ( _wrappedStream != null )
                    {
                        _wrappedStream.Dispose();
                    }
                }

                _wrappedStream = null;
                _disposed = true;

                ResourceManager.OnResourceCreationStreamDisposed( this );
            }
            
            base.Dispose( disposing );
        }
        #endregion
    }
}
